package com.liferay.controller;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.contant.Contants;
import com.liferay.model.ListSubjectOpen;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.service.ScheduleService;
import com.liferay.service.impl.ScheduleServiceImpl;

@Controller(value = "ScheduleController")
@RequestMapping("VIEW")
public class ScheduleController {

	private static final Logger log = Logger
			.getLogger(ScheduleController.class);
	private String mssv = "";
	private String mail = "";
	private ScheduleService scheduleService;
	private List<ListSubjectOpen> listSubjectOpen;

	@RequestMapping
	public String showViewSchedule(RenderRequest renderRequest,
			RenderResponse renderResponse, Model model) throws NumberFormatException, SystemException, PortalException {
		String userId = renderRequest.getRemoteUser();
		if(userId!=null){
			List<Role> listRole = RoleLocalServiceUtil.getUserRoles(Long.parseLong(userId));
			for(Role r : listRole){
				if (r.getName().equalsIgnoreCase(Contants.ROLE_STUDENT)) {
					User user = UserLocalServiceUtil.getUser(Long.parseLong(userId));
					mail =  user.getEmailAddress();
					scheduleService = new  ScheduleServiceImpl();
					listSubjectOpen = scheduleService.getListSubjectOpen(Contants.ROLE_STUDENT, mail);
					model.addAttribute("listSubjectOpen", listSubjectOpen);
					log.info("show mark");
					return "schedule";
				}
			}
		}
		return "view";
	}

	@ActionMapping(params = "action=scheduleforGuest")
	public void markforguest(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		mssv = ParamUtil.getString(actionRequest, "mssv");
		log.info("MSSV: " + mssv);
		mail = mssv + "@hcmuaf.edu.vn";
		actionResponse.setRenderParameter("action", "showScheduleForGuest");
	}

	@RenderMapping(params = "action=showScheduleForGuest")
	public String showScheduleForGuest(RenderRequest renderRequest,
			RenderResponse renderResponse, Model model) {
		scheduleService = new  ScheduleServiceImpl();
		listSubjectOpen = scheduleService.getListSubjectOpen("role_student", mail);
		String nameStudent = listSubjectOpen.get(0).getNameStudent();
		String yearOfSemester = listSubjectOpen.get(0).getYearOfSemester();
		int semester = listSubjectOpen.get(0).getSemester();
		String semes = String.valueOf(semester);
		model.addAttribute("listSubjectOpen",listSubjectOpen);
		model.addAttribute("nameStudent",nameStudent);
		List<String> list = new ArrayList<String>();
		list.add(nameStudent);
		list.add(yearOfSemester);
		list.add(semes);
		model.addAttribute("list",list);
		
		log.info(list.get(0));
		log.info(list.get(1));
		log.info(list.get(2));
 
		return "schedule";
	}

}
